#pragma once

#include "../im.h"
#include "IMSession.h"

class IMServer
{
public:
    IMServer(boost::asio::io_context &io_context, short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
    {
        do_accept();
    }

private:
    void do_accept()
    {
        acceptor_.async_accept(
            [this](boost::system::error_code ec, tcp::socket socket)
            {
                LOG_I << "do_accept " << socket.remote_endpoint().address().to_string()
                      << ":" << socket.remote_endpoint().port();
                if (!ec)
                {
                    std::make_shared<IMSession>(std::move(socket))->start();
                }
                do_accept();
            });
    }
    tcp::acceptor acceptor_;
};